<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport"
          content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">

    <title>{$mach['merchant']}-快速付款</title>
    <script src="__STATIC_URL__/static/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="__STATIC_URL__/static/mobile/css/weui.css" />
    <link rel="stylesheet" href="__STATIC_URL__/static/css/pay.css?id=13"/>
    <!--<link rel="stylesheet" href="__STATIC_URL__/static/css/mobile_pay.css?id=4"/>-->
    <script type="text/javascript" src="__STATIC_URL__/static/js/keyboard2.js?v=111"></script>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    <script src="__STATIC_URL__/static/js/app.js?v=9"></script>
    <script>
        wx.config({
            debug: false,
            appId: "{$jssdk['appId']}",
            timestamp: "{$jssdk['timestamp']}",
            nonceStr: "{$jssdk['nonceStr']}",
            signature: "{$jssdk['signature']}",
            jsApiList: [
                'hideAllNonBaseMenuItem'

            ]
        });
        wx.ready(function () {
            wx.hideAllNonBaseMenuItem();

        })
    </script>
    <style>

    </style>

</head>

<div>
    <div class="zt">
        <div class="zw"></div>
        <div style="width: 100%;" class="header">
            <div class="top">
                <div class="logo"><img src="{$mach['img']}"></div>
                <div class="mallinfo"><span >{$mach['merchant']}</span></div>
            </div>

            <div style="clear:both"></div>
        </div>
        <div class="zw"></div>
        <div class="cardexplain">

            <ul class="round">

                <li class="nob">
                    <table>

                        <tr>
                            <td>
                                <form role="form" id="tab">
                                    <div class="form-group">
                                        <span id="ti">金额(元)</span>
                                        <label for="name"><span class="span">￥</span></label>
                                        <input type="text" class="form-control" id="price" name="price"
                                               oninput="if(value.length>5)value=value.slice(0,5)" readonly="readonly">
                                        <!--<input type="hidden" name="openid" id="openid" value="{$openid}"/>-->
                                        <input type="hidden" name="attach" id="attach" value="qrcodezf"/>
                                        <input type="hidden" name="orderid" id="orderid" value=""/>
                                        <input type="hidden" name="sub_openid" id="sub_openid" value="{$openid}"/>
                                    </div>
                                </form>
                            <td>
                        </tr>
                    </table>


            </ul>
        </div>


        <!--<div class="foots"><span>支付完成后，如需退款请及时联系店员</span></div>-->


        <div style="clean:both"></div>
    </div>
</div>
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>
<script>
    (function () {

        var input1 = document.getElementById('price');


        new KeyBoard(input1,"","{$channel}");

        input1.onclick = function () {
            new KeyBoard(input1,"","{$channel}");

        };
    })();



    function getorder() {
        var id = {$id};
        $.ajax({
            type: "POST",
            url: "{:url('wxnocardpay/getorder2')}",

            cache: false,
            async: false,
            data: {
                id: id
            },


            success: function (data) {

                $('#orderid').val(data);

            }
        });

    }

    function zf() {
        var $loadingToast = $('#loadingToast');
        setTimeout(function () {
            $loadingToast.fadeOut(100);
        }, 100);
        $("#paytoBtn").prop("onclick",null).off("click");

        getorder();
        var price = $('#price').val();
        if (price < 0.01) {
            alert('金额不能小于0.01元');
            $('#price').focus();
        } else if (price > 20000) {
            alert('超过最大支付限额');
            $('#price').focus();
        }

        else {

            $.ajax({
                type: "POST",

                url: "/wxnocardpay/pay2/id/{$id}",

                cache: false,
                dataType: "json",
                async: false,
                data: $('#tab').serialize(),

                success: function (res) {

                    if (res.return_code ==="FAIL") {
                        alert(res.return_msg);
                        getorder();
                        var $loadingToast = $('#loadingToast');
                        setTimeout(function () {
                            $loadingToast.fadeOut(100);
                        }, 2000);
                        // $('#paytoBtn').attr('onclick', 'zf()');
                        $("#paytoBtn").attr("onclick", "sendpay();");
                        $('#paytoBtn').val('重新支付');

                    } else {
                        if (typeof WeixinJSBridge == "undefined") {
                            if (document.addEventListener) {
                                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                            } else if (document.attachEvent) {
                                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                            }
                        } else {
                            onBridgeReady(res.jsApiObj);
                        }
                    }

                }
            });

        }
    }

    function onBridgeReady(data) {

        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function (res) {
                WeixinJSBridge.log(res.err_msg);
                if (res.err_msg === 'get_brand_wcpay_request:ok') {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    paysuccess();

                } else if (res.err_msg === 'get_brand_wcpay_request:fail') {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    $('#price').val("");
                    $("#paytoBtn").attr("onclick","sendpay();");
                    $('#paytoBtn').addClass('disabled');
                    $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                    $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span>微信支付</span><b></b>');
                    alert(res.err_desc);
                    getorder();

                } else {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    $('#price').val("");
                    $("#paytoBtn").attr("onclick","sendpay();");
                    $('#paytoBtn').addClass('disabled');
                    $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                    $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span>微信支付</span><b></b>');

                    getorder();


                }

            });
    }

    function paysuccess() {
        var ordid = $('#orderid').val();

        $.ajax({
            type: "POST",

            url: "/wxnocardpay/payover/id/{$id}/orderid/" + ordid,

            cache: false,
            dataType: "json",
            async: false,
            data: {},

            success: function (data) {
                if (data === 0) {

                    window.location.replace("/wxnocardpay/orderinfo/id/{$id}/orderid/" + ordid);
                } else {

                    alert('支付失败');
                }
            }


        });

    }

</script>
</body>
</html>